/**
 * Created by Administrator on 2016/8/23.
 */
;(function () {

    //利用自定义组件构造左侧说明文本
    window.creatIChart = function (id, flow, scale) {
        var labels = ["1", "2", "3", "4", "5", "6", "7"];

        var baseData = {
            render: '',
            data: null,
            align: 'center',
            title: {
                text:'',
                font: '微软雅黑',
                fontsize: 14,
                color: '#b4b4b4'
            },
            subtitle: {
                text: '',
                font: '微软雅黑',
                color: '#b4b4b4'
            },
            footnote: {
                text: '',
                font: '微软雅黑',
                fontsize: 11,
                fontweight: 400,
                padding: '0 20',
                color: '#b4b4b4'
            },
            width: 580,
            height: 250,
            shadow: true,
            shadow_color: '#202020',
            shadow_blur: 8,
            shadow_offsetx: 0,
            shadow_offsety: 0,
            background_color: '#2e2e2e',
            tip: {
                enable: true,
                shadow: true,
                listeners: {
                    //tip:提示框对象、name:数据名称、value:数据值、text:当前文本、i:数据点的索引
                    parseText: function (tip, name, value, text, i) {
                        return "<span style='color:#005268;font-size:12px;'>值为：" +
                            "</span><span style='color:#005268;font-size:13px;'>" + value + "</span>";
                    }
                }
            },
            crosshair: {
                enable: true,
                line_color: '#ec4646'
            },
            sub_option: {
                smooth: true,// (smooth：光滑的，流畅的)
                label: false,
                hollow: false,//（hollow: 空的，虚伪的）
                hollow_inside: false,
                point_size: 8
            },
            coordinate: {   //坐标系的配置项
                striped_factor: 0.18,  //应用隔行变色后变色程度的因子,相对于background-color,值越大变化越大。(默认为'0.01')
                grid_color: '#4e4e4e',
                axis: {  //坐标轴轴线的配置项
                    color: '#252525',
                    width: [0, 0, 1,0]
                },
                scale: [{
                    position: 'left',
                    start_scale:0,
                    end_scale:100,
                    scale_space:10,
                    scale_width:100,  //刻度的长短。单位px
                    scale_size: 1, //刻度线的粗细
                    scale_share:6,
                    scale_enable: false,
                    text_space:1,//文本与坐标轴之间的距离。单位px。(默认为6)
                    label: {color: '#9d987a', font: '微软雅黑', fontsize: 11, fontweight: 400},
                    scale_color: '#9f9f9f'
                }, {
                    position: 'bottom',
                    label: {color: '#9d987a', font: '微软雅黑', fontsize: 11, fontweight: 400},
                    scale_enable: false,
                    labels: labels
                }]
            }
        };
        var data = [
            {
                name: 'PV',
                value: flow,
                color: '#ec4646',
                line_width: 2
            }
        ];
        baseData.render = id;
        baseData.data = data;
        if (scale && scale.start_scale) {
            baseData.coordinate.scale[0].start_scale = scale.start_scale;
        }
        if (scale && scale.scale_space) {
            baseData.coordinate.scale[0].scale_space = scale.scale_space;
        }
        baseData.coordinate.scale[0].end_scale = baseData.coordinate.scale[0].start_scale + baseData.coordinate.scale[0].scale_space * baseData.coordinate.scale[0].scale_share;

        // if(!isNaN(scale.end_scale) && !isNaN(scale.start_scale) && !isNaN(scale.scale_space) && scale.scale_space != 0) {
        //     baseData.coordinate.scale[0].scale_share = parseInt((scale.end_scale - scale.start_scale ) / scale.scale_space);
        // }

        var chart = new iChart.LineBasic2D(baseData);
        chart.plugin(new iChart.Custom({
            drawFn: function () {
                //计算位置
                var coo = chart.getCoordinate(),
                    x = coo.get('originx'),
                    y = coo.get('originy'),
                    w = coo.width,
                    h = coo.height;
                //在左上侧的位置，渲染一个单位的文字
                chart.target.textAlign('start')
                    .textBaseline('bottom')
                    .textFont('400 11px 微软雅黑')
                    .fillText('', x - 30, y - 5, false, '#9d987a')
                    .textBaseline('top')
                    .fillText('(T)', x + w + 5, y + h + 10, false, '#9d987a');

            }
        }));
        //开始画图
        chart.draw();
        return chart;
    }
})();
